package hit.example.Db;

import hit.example.Object.ObjWord;
import hit.example.adapter.MyCons;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;

public class DbAnhViet extends SQLiteOpenHelper {
	public static String DATABASE_NAME = Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ "/Hit_Dict/db/anh_viet.db";
	public static final int DATABASE_VERSION = 1;
	int ngonngu = 0;
	private String word = "";
	SQLiteDatabase myDatabase;
	Context myContext;
	String path = "";
	ArrayList<ObjWord> listWord;
	ObjWord obj;

	public String getWord() {
		return word;
	}

	public static DbAnhViet getInstance(int ngonngu, Context context) {
		if (ngonngu == 0) {
			DATABASE_NAME = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + "/Hit_Dict/db/anh_viet.db";
		} else {
			DATABASE_NAME = Environment.getExternalStorageDirectory()
					.getAbsolutePath() + "/Hit_Dict/db/viet_anh.db";
		}

		return new DbAnhViet(context);
	}

	public DbAnhViet(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

		this.myContext = context;

	}

	public void OpenDB() {
		path = DATABASE_NAME;
		File f = new File(path);
		if (!f.exists()) {
			Log.e("DB", "File khoong ton tai");
			try {

			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	}

	public void CloseDB() {
		myDatabase.close();
	}

	public SQLiteDatabase getReadableDatabase() {
		myDatabase = SQLiteDatabase.openDatabase(path, null,
				SQLiteDatabase.OPEN_READONLY|SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
		return myDatabase;
	}

	public SQLiteDatabase getWriteableDatabase() {
		myDatabase = SQLiteDatabase.openDatabase(path, null,
				SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
		return myDatabase;
	}

	public ArrayList<ObjWord> getDatabase(String key, int ngonngu) {
		String sql;

		OpenDB();
		getReadableDatabase();
		listWord = new ArrayList<ObjWord>();
		if (ngonngu == 0) {
			sql = "SELECT * FROM " + MyCons.TABLE_NAME_ENGLISH
					+ " where word >= '" + key + "' LIMIT 40";
		} else {
			sql = "SELECT * FROM " + MyCons.TABLE_NAME_VIET
					+ " where word >= '" + key + "' LIMIT 40";
		}
		Cursor c = myDatabase.rawQuery(sql, null);
		int iID = c.getColumnIndex("id");
		int iWord = c.getColumnIndex("word");
		int iContent = c.getColumnIndex("content");
		if (c.moveToFirst()) {
			do {
				obj = new ObjWord();
				obj.setiD(iID);
				obj.setiWord(c.getString(iWord));
				obj.setiContent(c.getString(iContent));
				listWord.add(obj);

			} while (c.moveToNext());
		}
		CloseDB();
		return listWord;
	}

	public String getWordClick(String word) {
		OpenDB();
		getReadableDatabase();
		String result = null;
		String sql;
		sql = "SELECT * FROM " + MyCons.TABLE_NAME_ENGLISH + " Where word='"
				+ word + "'";
		Cursor c = myDatabase.rawQuery(sql, null);

		int iContent = c.getColumnIndex("content");
		if (c.moveToFirst()) {
			// result = decodeData(c.getString(iContent));
			result = c.getString(iContent);
		}
		CloseDB();
		return result;
	}

	public int getCount(String word) {
		OpenDB();
		getReadableDatabase();
		int count;
		String sql;
		sql = "SELECT * FROM " + MyCons.TABLE_NAME_ENGLISH + " Where word='"
				+ word + "'";
		Cursor c = myDatabase.rawQuery(sql, null);

		count = c.getCount();
		CloseDB();
		return count;
	}

	public void onCreate(SQLiteDatabase arg0) {

	}

	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

	}

	public ArrayList<ObjWord> get(String word) {
		long time=System.currentTimeMillis();
		
		OpenDB();
		getReadableDatabase();
		listWord = new ArrayList<ObjWord>();
		Cursor c=myDatabase.query(MyCons.TABLE_NAME_ENGLISH, null,word, null,null,null,null);
		Log.d("dinhtv", "C :" + c.getCount());
		int iWord = c.getColumnIndex("word");
		int iContent = c.getColumnIndex("content");
		if (c.moveToFirst()) {
			do {
				obj = new ObjWord();
				obj.setiWord(c.getString(iWord));
				obj.setiContent(c.getString(iContent));
				listWord.add(obj);
			} while (c.moveToNext());
		}
		CloseDB();
		Log.d("dinhtv", "Time="+(System.currentTimeMillis()-time));
		return listWord;
	}

	public static String decodeData(String data) {
		if (data == null || data.length() < 6)
			return data;
		HashMap<String, Integer> hm = new HashMap<String, Integer>();
		String[] key = new String[] { "A", "B", "C", "D", "E", "F", "G", "H",
				"I", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "V",
				"X", "Y", "Z" };
		Integer[] value = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
				16, 17, 18, 19, 20, 21, 22, 23 };
		for (int i = 0; i < value.length; i++) {
			hm.put(key[i], value[i]);
		}
		String mdata = data;
		mdata = mdata.substring(4);
		int keyNext = hm.get("" + mdata.charAt(0));
		mdata = mdata.substring(keyNext + 1);
		String telDecom = null;
		try {
			byte[] data1 = Base64.decode(mdata, Base64.DEFAULT);
			telDecom = new String(data1, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// Mlog.E(TAG + " decodeData" + e);
		}
		return telDecom;
	}
	

}